﻿@*部门编辑页面*@

@{
    Layout = "_Layout";
    string id = (string)ViewData["id"];

    bool isAdd = (bool)ViewData["isAdd"];
}
<div style="margin: 15px;">
    <form class="layui-form" id="updateForm" action="@Url.Action("Edit","Department")">
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" disabled name="No" class="layui-input" data-bind="value:no">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编码</label>
            <div class="layui-input-block">
                <input type="text" name="code" lay-verify="required" data-bind="value:code" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" data-bind="value:name" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remarks" class="layui-textarea" data-bind="value:remarks"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="btnSubmit">提交</button>
            </div>
        </div>
    </form>
    <script type="text/javascript">
        function vModel() {
            this.id= ko.observable("");
            this.no = ko.observable("");
            this.name = ko.observable("");
            this.code = ko.observable("");
            this.parentId = ko.observable(""),
            this.remarks = ko.observable("");
        }
        var model= new vModel();
        layui.use(['element', 'layer', 'form'], function () {
            var $ = layui.jquery,
                form = layui.form;
            
            $.ajax({
                type: "Get",
                url: "/Department/Get?id="+'@id'+"&isAdd=@isAdd", //获取数据的ajax请求地址
                success: function (res) {
                    if (res.success) {
                        ko.mapping.fromJS(res.data, {}, model);
                        form.render();
                    } else {
                        admin_error(res.msg);
                    }
                }
            });
            //监听提交
            form.on('submit(btnSubmit)', function (data) {
                $.ajax({
                    url: $("#updateForm").attr("action"),
                    type: 'post',
                    dataType: 'json',
                    async: false,
                    data: model,//$("#updateForm").serialize(),
                    success: function (res) {
                        if (res.success) {
                            admin_success();
                            parent.layer.closeAll('iframe');
                        } else {
                            admin_error(res.msg);
                        }
                    }
                });
                return false;
            });

            $('.pick-icon').on('click', function () {
                layer.open({
                    type: 2,
                    title: "选择图标",
                    shadeClose: true,
                    shade: 0.4,
                    area: ['90%', '90%'],
                    content: "@Url.Action("GetIcon","Home")",
                    btn: ['确定','关闭'],
                    yes: function(index){
                        //当点击‘确定’按钮的时候，获取弹出层返回的值
                        var res = window["layui-layer-iframe" + index].callbackdata();
                        model.icon(res.icon_code);
                        //最后关闭弹出层
                        layer.close(index);
                    },
                    cancel: function(){
                        //右上角关闭回调
                    }
                });
            });
        });
        ko.applyBindings(model);//注册vModel
    </script>
</div>